package com.twu.biblioteca;

import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by lbutcher on 31/07/2014.
 */
public class Library {
    public List<Book> getListOfBooks() {
        return listOfBooks;
    }
    private List<Book> listOfBooks;

    public List<Movie> getListOfMovies() {
        return listOfMovies;
    }
    private List<Movie> listOfMovies;

    public Library() {
        // List of books
        listOfBooks = new ArrayList<Book>();
        listOfBooks.add(new Book("Book A", "Author A", "2014", false));
        listOfBooks.add(new Book("Book B", "Author B", "2013", false));
        listOfBooks.add(new Book("Book C", "Author C", "2014", false));

        // List of movies
        listOfMovies = new ArrayList<Movie>();
        listOfMovies.add(new Movie("Movie A", "2013", "Director A", 4, false));
        listOfMovies.add(new Movie("Movie B", "2010", "Director B", 9, false));
        listOfMovies.add(new Movie("Movie C", "2007", "Director C", 6, false));
    }

    public String returnProduct(String product) {
        String output = "";
        boolean checkedOut = lookForProductToCheckInOrReturn(product, true);
        if (checkedOut) {
            output = "That is not a valid product to return.";
        } else {
            output = "Thank you for returning the product";
        }

        return output;
    }

    public String checkOutProduct(String product) {
        String output = "";
        boolean checkedOut = lookForProductToCheckInOrReturn(product, false);
        if (checkedOut) {
            output = "Thank you! Enjoy the product";
        } else {
            output = "That product is not available";
        }

        return output;
    }

    public boolean lookForProductToCheckInOrReturn(String input, boolean checkedOut) {
        boolean foundProduct = false;

        for(Book book : listOfBooks) {
            // If book title has been found and checked out flag complies
            if(book.getTitleOfBook().equals(input) && (book.isBookCheckedOut() == checkedOut)) {
                toggleCheckOutBook(book);
                checkedOut = book.isBookCheckedOut();
                foundProduct = true;
            }
        }

        // If book not found, look through movies
        if(!foundProduct) {
            for(Movie movie : listOfMovies) {
                // If movie title has been found and checked out flag complies
                if(movie.getMovieTitle().equals(input) && (movie.isMovieCheckedOut() == checkedOut)) {
                    toggleCheckOutMovie(movie);
                    checkedOut = movie.isMovieCheckedOut();
                }
            }
        }

        return checkedOut;
    }

    public void toggleCheckOutBook(Book bookToCheckOut) {
        if (bookToCheckOut.isBookCheckedOut()) {
            bookToCheckOut.setBookCheckedOut(false);
        } else {
            bookToCheckOut.setBookCheckedOut(true);
        }
    }

    public void toggleCheckOutMovie(Movie movieToCheckOut) {
        if (movieToCheckOut.isMovieCheckedOut()) {
            movieToCheckOut.setMovieCheckedOut(false);
        } else {
            movieToCheckOut.setMovieCheckedOut(true);
        }
    }

    public String listBooks() {
        String output = "Title     Author      Year Published\n";
        for(Book book : listOfBooks) {
            // If book hasn't been checked out
            if(!book.isBookCheckedOut()) {
                output += book.getTitleOfBook() + "    " + book.getAuthorOfBook() + "    "
                        + book.getYearPublished() + "\n";
            }
        }

        return output;
    }

    public String listMovies() {
        String output = "Title     Year   Director     Rating\n";
        for(Movie movie : listOfMovies) {
            // If movie hasn't been checked out
            if(movie.isMovieCheckedOut() == false) {
                output += movie.getMovieTitle() + "   " + movie.getYearOfRelease() + "   " +
                        movie.getDirectorOfMovie() + "     " + movie.getMovieRating() + "\n";
            }
        }

        return output;
    }
}
